package com.hejia.alauda.persistence.mybatis;

import java.util.Date;
import java.util.List;

import com.hejia.alauda.entity.BonusRecord;
import com.hejia.alauda.entity.ProductCashSettlement;
import com.hejia.alauda.entity.RepaymentBonus;
import com.hejia.alauda.entity.RepaymentLedger;
import com.hejia.alauda.entity.RepaymentOrder;
import com.hejia.alauda.entity.RepaymentOrder.RepayExecuteStatus;
import com.hejia.alauda.persistence.BaseMapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Update;

public interface RepaymentOrderMapper extends BaseMapper<RepaymentOrder> {

    Integer amountOfRepay(@Param("date")String date, @Param("status")RepaymentOrder.Status status);

    /**
     * 查询用户当天同类红包打款申请订单
     * @param userId 用户ID
     * @param bonusrecordCode	红包策略编码
     * @return
     */
    @Select("select r.* from repaymentorder r join bonusorder b on b.repayOrder_id = r.id join bonusrecord br on br.id = b.bonusRecord_id where r.financingIds = '' and r.status = 'STATUS_APPLY' and b.repaid = 0 and b.inPay = 1 and date(r.createDate) = date(now()) and r.userId = #{userId} and br.code = #{bonusrecordCode} limit 0, 1;")
	@ResultMap("DetailMap")
    RepaymentOrder findUserTodayAppliedBonusRepaymentOrder(
    		@Param("userId") Long userId, 
    		@Param("bonusrecordCode") String bonusrecordCode);

    /**
     * 更新还款订单为等待执行状态
     * @param ids
     * @return
     */
    Integer updateRepaymentOrderAwaitingrExecutionPay(@Param("ids") List<Long> ids);

    /**
     * 更新还款订单打款执行状态
     * @param id 还款订单ID
     * @param repayExecuteStatus 还款执行状态
     * @return
     */
	@Update("update repaymentorder set repayExecuteStatus = #{repayExecuteStatus} where id = #{id}")
    Integer updateRepaymentOrderRepayExecuteStatus(
    		@Param("id") Long id, 
    		@Param("repayExecuteStatus") RepayExecuteStatus repayExecuteStatus);

	/**
	 * 查询打款明细
	 * @param createDate 申请日期
	 * @return	打款明细
	 */
	@Select("select r.id 'repaymentOrderId', r.userId, r.description, f.product_id 'productId', f.name 'productName', r.userName,"
			+ " r.amount 'repayAmount', f.amount 'financingAmount', ceil((f.amount * f.interestRate / 360 * f.term) * 100) / 100 'incomeAmount',"
			+ " if(b.redEnvelope >= 50, 50, 0) 'register50RedEnvelope',"
			+ " if(b.redEnvelope < 50, redEnvelope, redEnvelope - 50) 'financialRedEnvelope'"
			+ " from repaymentorder r join financing f on r.financingIds = f.id join balance b on r.userId = b.id"
			+ " where date(r.createDate) = date(#{createDate})"
			+ " order by f.name, r.userName")
	List<RepaymentLedger> findRepaymentLedger(@Param("createDate") Date createDate);

	/**
	 *  查询打款红包
	 * @param createDate 申请日期
	 * @return
	 */
	@Select("select r.id 'repaymentOrderId', br.description, br.code, count(bo.id) bonusOrderCount, sum(bo.amount) amount"
			+ " from repaymentorder r join bonusorder bo on bo.repayOrder_id = r.id"
			+ " join bonusrecord br on br.id = bo.bonusRecord_id"
			+ " where date(r.createDate) = date(#{createDate})"
			+ " group by r.id, br.code")
	List<RepaymentBonus> findRepaymentBonus(@Param("createDate") Date createDate);

	/**
	 * 查询产品兑付结算
	 * @param settlementDate 兑付日
	 * @return	产品兑付结算列表
	 */
	@Select("select f.name, date(f.valueDate) 'valueDate', f.term, f.settlementDate, sum(f.amount) 'amount',"
			+ " (sum(f.amount) * f.interestRate / 360 * f.term) income, count(f.id) count,"
			+ " p.interestRate, (sum(f.amount) * p.interestRate / 360 * f.term) interestIncome,"
			+ " p.bonusRate, (sum(f.amount) * p.bonusRate / 360 * f.term) bonusIncome"
			+ " from financing f join product p on f.product_id = p.id"
			+ " where date(f.settlementDate) = date(#{settlementDate})"
			+ " group by f.name, date(f.valueDate)"
			+ " order by f.name, f.valueDate")
	List<ProductCashSettlement> findProductCashSettlements(Date settlementDate);

	/**
	 * 查询红包兑付列表
	 * @param applyDate 申请日期
	 * @return
	 */
	@Select("select br.* "
			+ " from bonusrecord br join bonusorder bo on bo.bonusRecord_id = br.id"
			+ " join repaymentorder ro on bo.repayOrder_id = ro.id"
			+ " where date(ro.createDate) = #{applyDate} and ro.financingIds = ''"
			+ " order by br.code, br.user_id")
	List<BonusRecord> findCashBonus(Date applyDate);

	List<RepaymentOrder> findApplyRepaymentOrderByKeywordOrDate(
			@Param("startNumber") Integer startNumber, 
			@Param("endNumber") Integer endNumber, 
			@Param("applyDate") String applyDate, 
			@Param("keyword") String keyword, 
			@Param("showBonus") Boolean showBonus);

	Integer countApplyRepaymentOrderByKeywordOrDate(
			@Param("applyDate") String applyDate, 
			@Param("keyword") String keyword, 
			@Param("showBonus") Boolean showBonus);
}
